/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import entity.MemberCustomer;
import entity.SaleOrder;
import entity.SaleOrderFacadeLocal;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

/**
 *
 * @author SONY
 */
@Stateless
public class SaleOrderService implements SaleOrderServiceLocal {
    
    @EJB SaleOrderFacadeLocal saleOrderFacadeLocal;

    @Override
    public List<SaleOrder> getAllSaleOrders_byMemberID(int memberid) {
        List<SaleOrder> lstSaleOrders_bymemberid = new ArrayList<SaleOrder>();
        try {
            List<SaleOrder> lstSaleOrders = saleOrderFacadeLocal.findAll();
            for(int i = 0; i < lstSaleOrders.size(); i++) {
                MemberCustomer member = lstSaleOrders.get(i).getMemberID();
                if(member == null) {
                    continue;
                }else if(member.getMemberID() == memberid) {
                    SaleOrder sale_order = lstSaleOrders.get(i);
                    lstSaleOrders_bymemberid.add(sale_order);
                }
            }          
        } catch (Exception e) {
            System.err.println(e);
        }
        return lstSaleOrders_bymemberid;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public SaleOrder addSaleOrder(MemberCustomer MemberID, String Name, String Address, String Phone, Date OrderDate, String Status, BigDecimal Total, String PaymentType) {
        try {
            //add SaleOrder info into SaleOrder table
            SaleOrder saleorder = new SaleOrder();
            saleorder.setSaleOrderID(1);
            saleorder.setMemberID(MemberID);
            saleorder.setCustomerName(Name);
            saleorder.setShippingAddress(Address);
            saleorder.setShippingPhone(Phone);
            saleorder.setOrderDate(OrderDate);
            saleorder.setStatus(Status);
            saleorder.setTotalPrice(Total);
            saleorder.setPaymentType(PaymentType);
            saleOrderFacadeLocal.create(saleorder);
            
            //get SaleOrder object just add into table
            List<SaleOrder> lstSaleOrders = saleOrderFacadeLocal.findAll();
            int index = lstSaleOrders.size() - 1;
            SaleOrder saleorder_new = lstSaleOrders.get(index);
                        
            return saleorder_new;
        } catch (Exception e) {
            System.err.println(e);
        }
        return null;
    }

    @Override
    public List<SaleOrder> getAllSaleOrders() {
        return saleOrderFacadeLocal.findAll();
    }

    @Override
    public SaleOrder getSaleOrder_Details_bySaleOrderID(int saleorderid) {
        return saleOrderFacadeLocal.find(saleorderid);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean updateStatus_bySaleOrderID(int saleorderid, String status) {
        try {
            SaleOrder saleorder = saleOrderFacadeLocal.find(saleorderid);
            saleorder.setStatus(status);
            saleOrderFacadeLocal.edit(saleorder);
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

    @Override
    public List<SaleOrder> getAllSaleOrders_LikeOID(String oid) {
        List<SaleOrder> lstSaleOrders = new ArrayList<SaleOrder>();
        List<SaleOrder> lstSaleOrders_find = saleOrderFacadeLocal.findAll();
        for(int i = 0; i < lstSaleOrders_find.size(); i++) {
            SaleOrder saleorder = lstSaleOrders_find.get(i);
            if(saleorder.getSaleOrderID().toString().contains(oid)) {
                lstSaleOrders.add(saleorder);
            }
        }
        return lstSaleOrders;
    }

    @Override
    public List<SaleOrder> getAllSaleOrders_byStatus(String status) {
        List<SaleOrder> lstSaleOrders = new ArrayList<SaleOrder>();
        List<SaleOrder> lstSaleOrders_find = saleOrderFacadeLocal.findAll();
        for(int i = 0; i < lstSaleOrders_find.size(); i++) {
            SaleOrder saleorder = lstSaleOrders_find.get(i);
            if(saleorder.getStatus().equals(status)) {
                lstSaleOrders.add(saleorder);
            }
        }
        return lstSaleOrders;
    }
}
